<template>
    <!-- 总容器 -->
    <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
            <p>确认您的订单</p>
            <div></div>
        </header>
        <div class="top-ban"></div>

        <section>
            <div class="title">
                <p>体检人信息</p>
            </div>
            <table>
                <tr>
                    <td>姓名:</td>
                    <td>{{users.realName}}</td>
                </tr>
                <tr>
                    <td>证件号码:</td>
                    <td>{{users.identityCard}}</td>
                </tr>
                <tr>
                    <td>出生日期:</td>
                    <td>{{users.birthday.substring(0,10)}}</td>
                </tr>
                <tr>
                    <td>手机号码:</td>
                    <td>{{users.userId}}</td>
                </tr>
            </table>
            <div class="title">
                <p>体检日期</p>
            </div>
            <table>
                <tr>
                    <td>{{selectDay}}</td>
                </tr>
            </table>
            <div class="title">
                <p>体检机构</p>
            </div>
            <table>
                <tr>
                    <td colspan="2">{{hospital.name}}</td>
                </tr>
                <tr>
                    <td>营业时间:</td>
                    <td>{{hospital.businessHours}}</td>
                </tr>
                <tr>
                    <td>采血截止:</td>
                    <td>{{hospital.deadline}}</td>
                </tr>
                <tr>
                    <td>机构电话:</td>
                    <td>{{hospital.telephone}}</td>
                </tr>
                <tr>
                    <td>机构地址:</td>
                    <td>{{hospital.address}}</td>
                </tr>
            </table>
            <div class="title">
                <p>套餐类型</p>
            </div>
            <table>
                <tr>
                    <td>{{setmeal.name}}</td>
                </tr>
            </table>
        </section>
        <div ref="alipayForm"></div>
        <div class="bottom-btn">
            <div class="first">实付款: <span>￥{{setmeal.price}}</span></div>
            <div class="last" @click="appointmentsuccess">确认支付</div>
        </div>

        <div class="bottom-ban">
        <Foot></Foot>
        </div>
    </div>

</template>

<script>
import Foot from '../components/Foot.vue'
import { toRefs,toRef,reactive,ref, useId } from 'vue';
import axios from '../axios';
import { useRouter,useRoute } from 'vue-router';
import { getSessionStorage } from '../common';

export default {
    components:{Foot},
    setup(){

        const route = useRoute()
        const router = useRouter()
        const alipayForm = ref()

        const state = reactive({
            hpId: route.query.hpId,
            smId: route.query.smId,
            selectDay: route.query.selectDay,
            users: getSessionStorage('users'),
            hospital: {},
            setmeal: {}
        })

        loadHospital()
        loadsetmeal()

        function loadHospital(){
            axios.post('/api/getHospitalById', {
                        hpId:state.hpId
                    },{
                    headers: {
                        'token': getSessionStorage("token")
                    }
                    })
                    .then(function (response) {         
                        state.hospital = response.data.data
                    })
                    .catch(function (error) {
                    console.log(error);
                    })
        }

        function loadsetmeal(){
            axios.post('/api/getSetmealById', {
                        smId:state.smId
                    },{
                    headers: {
                        'token': getSessionStorage("token")
                    }
                    })
                    .then(function (response) {
                        state.setmeal = response.data.data
                    })
                    .catch(function (error) {
                    console.log(error);
                    })
        }

        function appointmentsuccess(){
            //访问接口 插入订单数据 
            axios.post('/api/createOrder', {
                    hpId: state.hpId,
                    orderDate: state.selectDay,
                    smId: state.smId,
                    state: 1,
                    userId: state.users.userId,
                    orderId: (state.hpId+state.smId+(Math.floor(Math.random() * 10000))+state.users.userId).substring(0,10)
                    // orderPrice: state.setmeal.price,
                    },{
                    headers: {
                        'token': getSessionStorage("token")
                    }
                    })
                    .then(function (response) {
                    const result = response.data;
                    console.log(result)
                    if (result.status == 200) {
                    window.open(`api/alipay/pay?subject=${state.users.realName}&traceNo=${result.data.orderId}&totalAmount=${state.setmeal.price}`, "_self");
                    } else {
                    alert(result.desc)
                    }
                    })
                    .catch(function (error) {
                    console.log(error);
                    })
                    .finally(function () {
                    });
            // axios.post('/api/createOrder', {
            //             smId:state.smId,
            //             orderDate:state.selectDay,
            //             hpId:state.hpId,
            //             state:1,
            //             userId:state.users.userId
            //         })
            //         .then(function (response) {
            //             console.log(response)
            //             if(response.data.status != 205){
            //                 console.log(response.data.status)
            //                 // toAlipay()
            //                 router.push({name:"AppointmentSuccess"})
            //             }else{
            //                 alert(response.data.desc)
            //             }
            //         })
            //         .catch(function (error) {
            //         console.log(error);
            //         })
        }

        // function toAlipay(){
        //     axios.post('/api/alipay/pay',{
        //         smId:state.smId,
        //         orderDate:state.selectDay,
        //         hpId:state.hpId,
        //         state:1,
        //         userId:state.users.userId
        //     })
        //     .then((response) => {
        //     console.log("获取的数据: "+response.data);
            
        //     // 将form表单字符串添加到<div>中
        //     alipayForm.value.innerHTML = response.data
        //     // console.log(alipayForm.value);
        //     //提交
        //     if(alipayForm.value.innerHTML == 205){
        //         alert("您当天已有预约,订单创建失败")
        //     }else{
        //         window.document.forms[0].submit()
        //     }
        //     })
        //     .catch((error) => {
        //         console.log(error);
        //     });
        // }


        return{
            ...toRefs(state),
            alipayForm,
            loadHospital,
            loadsetmeal,
            appointmentsuccess,
        }
    }
}
</script>

<style scoped>
/*********************** 总容器 ***********************/
.wrapper{
    width: 100%;
    height: 100%;
    background-color: #F9F9F9;
}

/*********************** header ***********************/
header{
    width: 100%;
    height: 15.7vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}
header .fa{
    font-size: 8vw;
}
/*********************** common样式 ***********************/
.top-ban{
    width: 100%;
    height: 15.7vw;
}
.bottom-ban{
    width: 100%;
    height: 26.2vw;
}

/*********************** section ***********************/
section{
    width: 86vw;
    margin: 0 auto;
}
section .title{
    width: 100%;
    height: 12vw;
    border-bottom: solid 1px #EEE;

    display: flex;
    align-items: center;
}
section .title p{
    height: 3.4vw;
    line-height: 3.4vw;
    font-size: 4.2vw;
    font-weight: 600;
    box-sizing: border-box;
    padding-left: 3vw;
    border-left: solid 2px #127A90;
}
section table{
    font-size: 3.6vw;
    color: #555;
    margin-top: 2vw;
}
section table tr{
    line-height: 8vw;
}
section table tr td:first-child{
    width: 22%;
}

/*********************** bottom-btn ***********************/
.bottom-btn{
    width: 100%;
    height: 12vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    bottom: 14.2vw;

    display: flex;
}
.bottom-btn .first{
    flex: 2;
    line-height: 12vw;
    font-size: 4.6vw;
    box-sizing: border-box;
    padding-left: 6vw;
}
.bottom-btn .first span{
    color: #F77B2D;
}
.bottom-btn .last{
    flex: 1;
    background-color: #117C94;
    line-height: 12vw;
    text-align: center;
    font-size: 5vw;
    color: #FFF;

    user-select: none;
    cursor: pointer;
}
</style>